<?php
require('config.php');
function mkdirs($dir, $mode = 0777)
{
    if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE;
    if (!mkdirs(dirname($dir), $mode)) return FALSE;
    return @mkdir($dir, $mode);
} 
function saveImage($path)
 {  
 	if(!preg_match('/\/([^\/]+\.[a-z]{3,4})$/i',$path,$matches))
 	{
 		//die('Use image please');
 	} 
 		//$image_name = strToLower($matches[1]);
 	  	$ch = curl_init ($path);
 	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 	    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
 	    $img = curl_exec ($ch);  curl_close ($ch);
 	    $time2=date('Y-m');
 	    $time3=time();
 	    $destination_folder='../upload/users/'.$time2.'/';
 	    mkdirs($destination_folder);
 	    $fp = fopen($destination_folder.$time3.'.jpg','w');
 	    fwrite($fp, $img);
 	    fclose($fp); 
 	    $fwq_name='upload/users/'.$time2.'/'.$time3.'.jpg';
 	    return $fwq_name;
 } 
        $mobile=$_REQUEST['mobile'];
        $pwd=$_REQUEST['pwd'];
        $type='';
		$sql="select * from `".$oto."_users` where `userPhone`='".$mobile."' and userFlag='1' LIMIT 1";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$cha_mobile[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(!isset($cha_mobile))
		{
			$arr=range(0,9);
			shuffle($arr);
			$sjs=$arr[0].$arr[1].$arr[2].$arr[3];
			$pwd=md5($pwd.$sjs);
			$time=date('Y-m-d H:i:s');
			if(isset($_REQUEST['type'])) $type=$_REQUEST['type'];
			if($type=='weixin')
			{
				$openid=$_REQUEST['openid'];
				$userName=$_REQUEST['userName'];
				$img=$_REQUEST['img'];
				$img=saveImage($img); 
				$sql="INSERT INTO ".$oto."_users (`loginSecret`,`loginName`,`loginPwd`,`userPhone`,`createTime`,`userName`,`openid`,`lastTime`,`userPhoto`) VALUES ('$sjs','$mobile','$pwd','$mobile','$time','$userName','$openid','$time','$img')";
			}
			else
			{
				$sql="INSERT INTO ".$oto."_users (`loginSecret`,`loginName`,`loginPwd`,`userName`,`userPhone`,`createTime`) VALUES ('$sjs','$mobile','$pwd','$mobile','$mobile','$time')";
			}
            $result=$db->query($sql);
            file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
            if($result)
            {
            	$data[0]['pan']=1;
            	$data[0]['msg']='注册成功';
            	if($type=='weixin')
            	{
            		$sql="select * from `".$oto."_users` where `openid`='{$openid}' and userFlag='1' LIMIT 1";
					$result=$db->query($sql);
					while($row=$result->fetch_assoc()){
						$pson[]=$row;//将取得的所有数据赋值给person_info数组
					}
					if(isset($pson))
					{
						$pson[0]['type']='weixin';
						$pson[0]['pan']=1;
            			$pson[0]['msg']='注册成功';
						$data=$pson;
					}
					else $data[0]['type']='';
            	} 

            }
            else
            {
            	$data[0]['pan']=2;
            	$data[0]['msg']='注册失败';
            	$data[0]['type']='';
            } 
		}
		else
		{
			$data[0]['pan']=-1;
			$data[0]['msg']='账号已存在';
			$data[0]['type']='';
		}
		echo json_encode($data);
?>